<?php

namespace App\Http\Controllers;

use App\Models\Link;
use App\Models\Topic;
use App\Models\TopicCategory;
use App\Models\User;
use Illuminate\Http\Request;

class TopicCategoriesController extends Controller
{
    public function show(TopicCategory $topicCategory, Request $request, Topic $topic, User $user, Link $link)
    {
        // 读取分类 ID 关联的话题，并按每 20 条分页
        $topics = $topic->withOrder($request->order)
            ->where('category_id', $topicCategory->id)
            ->with('user', 'category')   // 预加载防止 N+1 问题
            ->paginate(20);

        // 活跃用户列表
        $active_users = $user->getActiveUsers();
        // 资源链接
        $links = $link->getAllCached();

        // 传参变量话题和分类到模板中
        return view('topics.index', [
            'topics'       => $topics,
            'category'     => $topicCategory,
            'active_users' => $active_users,
            'links'        => $links,
        ]);
    }
}
